<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\ocean;

use app\enum\OeCodeEnum;
use app\enum\OeUrlEnum;
use proxy\Client;

class AdService extends OceanService
{
    /**
     * 项目搭建.
     *
     * @param array $headers 公共头
     * @param array $params 请求参数
     * @return bool|mixed|string
     */
    public static function createProject(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = OeUrlEnum::getServiceName('ad');
            $serviceUrl = OeUrlEnum::getServiceUrl('ad', 'project_create');
            return $client->postJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 关闭项目状态
     *
     * @param array $headers 公共头
     * @param array $params 请求参数
     * @return bool|mixed|string
     */
    public static function updateProjectStatus(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = OeUrlEnum::getServiceName('ad');
            $serviceUrl = OeUrlEnum::getServiceUrl('ad', 'project_update_status');
            $res = $client->postJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
            if (intval($res['code']) != OeCodeEnum::SUCCESS) {
                return self::setError($res['message']);
            }

            return $res;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 广告搭建.
     *
     * @param array $headers 公共头
     * @param array $params 请求参数
     * @return bool|mixed|string
     */
    public static function createAd(array $headers = [], array $params = [])
    {
        try {
            $client = new Client();
            $serviceName = OeUrlEnum::getServiceName('ad');
            $serviceUrl = OeUrlEnum::getServiceUrl('ad', 'ad_create');
            return $client->postJson($serviceName, $serviceUrl, $params)->setHeader($headers)->exec(Client::RETURN_TYPE);
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }
}
